package com.dxtx.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by user on 2017/4/18.
 */
public class BottomAlphaRecyclerView extends RecyclerView {
    private Paint paint;
    LinearGradient linearGradient;
    RectF rectF = new RectF();

    public BottomAlphaRecyclerView(Context context) {
        super(context);
        init();
    }

    public BottomAlphaRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //底部20%渐变消失,0xe0000000这个数字仅仅e0透明度有用
        linearGradient = new LinearGradient(w / 2, h * 0.8f, w / 2, h, Color.TRANSPARENT, 0xe0123456, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
        rectF.set(0, h * 0.8f, w, h);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        RectF bounds = new RectF(canvas.getClipBounds());
        int count = canvas.saveLayer(bounds, null, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        canvas.drawRect(rectF, paint);
        canvas.restoreToCount(count);
    }
}
